# -*- coding: utf-8 -*-
import requests
import lxml.html
import time

#爬取10页的图片进行保存
for page in range(0,5):
    
    header={
            'User-Agent': 'Chrome/94.0.4606.61 Safari/537.36'}
    #构造url
    url='https://movie.douban.com/celebrity/1001373/photos/?type=C&start={}&sortby=like&size=a&subtype=a'.format(page*30)
    source=requests.get(url,headers=header)
    html=source.content
    selector=lxml.html.fromstring(html)
    #获得一个页面的全部图片链接
    image_url=selector.xpath('///div[@class="cover"]/a/img/@src')
#    规范化处理
    image_url= [value.replace(" ", "").replace("\t", "") for value in image_url]
    #定义图片名称，防止覆盖
    
    #依次请求每一个url
    for i in range(len(image_url)):
        img_html=requests.get(image_url[i],headers=header)
#        #指定保存路径
        path=f"F:\Python作业\python爬虫作业\爬取泰勒斯威夫特图片、视频\photo\{str(page)+str(i)+'.jpg'}"
        #保存图片#（设置保存路径）
        with open(path,'wb') as f:
            f.write(img_html.content)
        
time.sleep(2)
            
      
        
        
  
        
